# -*- coding: utf-8 -*-
# ===========================================
# @Time    : 2021/10/9 10:26 
# @Author  : shutao
# @FileName: resolve_dxf.py
# @remark  : 
# 
# @Software: PyCharm
# Github 　： https://github.com/NameLacker
# ===========================================

# SECTION 开始标志
SECTION_START_KEY = b'  0\r\n'
SECTION_START_NAME = b'SECTION\r\n'
# SECTION 结束标志
SECTION_END_KEY = b'  0\r\n'
SECTION_END_NAME = b'SECTION\r\n'

# HEADERS 段
HEADERS_PART_KEY = b'  2\r\n'
HEADERS_PART_NAME = b'HEADER\r\n'

# CLASSES 段
CLASSES_PART_KEY = b'  2\r\n'
CLASSES_PART_NAME = b'CLASSES\r\n'

# TABLES 段
TABLES_PART_KEY = b'  2\r\n'
TABLES_PART_NAME = b'TABLES\r\n'

# BLOCKS 段
BLOCKS_PART_KEY = b'  2\r\n'
BLOCKS_PART_NAME = b'BLOCKS\r\n'

# ENTITIES 段
ENTITIES_PART_KEY = b'  2\r\n'
ENTITIES_PART_NAME = b'ENTITIES\r\n'

# OBJECTS 段
OBJECTS_PART_KEY = b'  2\r\n'
OBJECTS_PART_NAME = b'OBJECTS\r\n'

# THUMBNAILIMAGE 段
THUMBNAILIMAGE_PART_KEY = b'  2\r\n'
THUMBNAILIMAGE_PART_NAME = b'THUMBNAILIMAGE\r\n'


filepath = "E:\\project_c++\\resolve_dxf\\dxfFiles\\0001.dxf"


def run_main():
    # section开关
    section_lock = False

    # 打开并读取 dxf 文件（二进制）
    with open(filepath, 'rb') as f:
        dataset = f.readlines()

    # 获取键值总行数
    n_data = len(dataset)
    for i in range(0, n_data, 2):
        # 读取组码键值对
        key, value = dataset[i], dataset[i+1]

        # 读取主函数
        if section_lock:
            if key == HEADERS_PART_KEY and value == HEADERS_PART_NAME:
                # todo: 处理 HEADER 段数据
                print("{}--{}".format(key, value))

            if key == CLASSES_PART_KEY and value == CLASSES_PART_NAME:
                # todo: 处理 CLASSES 段数据
                print("{}--{}".format(key, value))

            if key == TABLES_PART_KEY and value == TABLES_PART_NAME:
                # todo: 处理 TABLES 段数据
                print("{}--{}".format(key, value))

            if key == BLOCKS_PART_KEY and value == BLOCKS_PART_NAME:
                # todo: 处理 BLOCKS 段数据
                print("{}--{}".format(key, value))

            if key == ENTITIES_PART_KEY and value == ENTITIES_PART_NAME:
                # todo: 处理 ENTITIES 段数据
                print("{}--{}".format(key, value))

            if key == OBJECTS_PART_KEY and value == OBJECTS_PART_NAME:
                # todo: 处理 OBJECTS 段数据
                print("{}--{}".format(key, value))

            if key == THUMBNAILIMAGE_PART_KEY and value == THUMBNAILIMAGE_PART_NAME:
                # todo: 处理 THUMBNAILIMAGE 段数据
                print("{}--{}".format(key, value))

            section_lock = False

        # 读取section
        if key == SECTION_START_KEY and value == SECTION_START_NAME:
            section_lock = True
            continue


if __name__ == '__main__':
    run_main()
